<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 类型 -->
			<cl-filter label="类型">
				<cl-select :options="options.type" prop="type" style="width: 120px" />
			</cl-filter>
			<!-- 状态 -->
			<cl-filter label="状态">
				<cl-select :options="options.status" prop="status" style="width: 120px" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="app-banner" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { reactive } from "vue";

const { service } = useCool();

// 数据选项
const options = reactive({
	type: [
		{
			label: "事件",
			value: "event",
			type: "success"
		},
		{
			label: "页面",
			value: "navigateTo",
			type: "warning"
		},
		{
			label: "程序",
			value: "ToMiniProgram",
			type: "danger"
		}
	],
	status: [
		{
			label: "禁用",
			value: 0,
			type: "danger"
		},
		{
			label: "正常",
			value: 1,
			type: "success"
		}
	]
})

// cl-upsert
const Upsert = useUpsert({
	items: [
		{ prop: "image", label: "图  片", component: { name: "cl-upload" }, required: true },
		{
			label: "路  径",
			prop: "url",
			span: 12,
			component: {
				name: "el-input",
				props: {
					placeholder: "pages/index/index?id=1"
				}
			}
		},
		{ prop: "title", label: "标  题", span: 12, component: { name: "el-input" } },
		{ prop: "appid", label: "APPID", span: 12, component: { name: "el-input" } },
		{ prop: "text", label: "文  本", span: 12, component: { name: "el-input" } },
		{
			prop: "type",
			label: "类 型",
			span: 12,
			component: {
				name: "el-select",
				options: [
					{ label: "点击事件", value: 'event' },
					{ label: "跳转页面", value: 'navigateTo' },
					{ label: "跳小程序", value: 'ToMiniProgram' },
				]
			},
			value: "event",
			required: true
		},
		{
			label: "状  态",
			prop: "status",
			span: 12,
			value: 1,
			component: {
				name: "el-select",
				options: [
					{ label: "正常", value: 1 },
					{ label: "禁用", value: 0 }
				]
			},
			required: true
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ prop: "id", label: "ID", width: 60 },
		{ prop: "image", label: "图片", component: { name: "cl-image", props: { size: 60 } } },
		{ prop: "url", label: "路径" },
		{
			prop: "type",
			label: "类型",
			dict: [
				{ label: "事件", value: "event", type: "success" },
				{ label: "页面", value: "navigateTo", type: "warning" },
				{ label: "程序", value: "ToMiniProgram", type: "danger" }
			]
		},
		{
			label: "状态",
			prop: "status",
			dict: [
				{ label: "禁用", value: 0, type: "danger" },
				{ label: "正常", value: 1, type: "success" }
			]
		},
		{ prop: "updateTime", label: "更新时间", sortable: "custom", width: 160 },
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.app.banner
	},
	(app) => {
		app.refresh();
	}
);
</script>
